package ru.sushka.lunch.gui.views;

import java.util.Calendar;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Table;

import ru.sushka.lunch.gui.AbstractMenuController;

import org.eclipse.swt.graphics.Point;

public class MenuForm {
    

    private DateTime dateTimeWidget;
    private Label dateTimeLabel;
    private Table menuTable;
    private Table courseTable;
    private Button removeMenuBtn;
    private Button saveMenuBtn;
    private Button cancelBtn;
    private Button addMenuBtn;
    
    private AbstractMenuController menuController;


    public MenuForm(AbstractMenuController menuController){
        super();
        this.menuController = menuController;
    }
    
    public DateTime getDateTimeWidget() {
        return dateTimeWidget;
    }

    public Label getDateTimeLabel() {
        return dateTimeLabel;
    }

    public Table getMenuTable() {
        return menuTable;
    }

    public Table getCourseTable() {
        return courseTable;
    }

    /**
     * Open the window.
     * @wbp.parser.entryPoint
     */
    public void open() {
        Display display = Display.getDefault();
        final Shell shell = new Shell();
        shell.setMinimumSize(new Point(620, 300));
        shell.setSize(620, 500);
        shell.setText("Заказ");
        shell.setLayout(new GridLayout(1, false));
        
        Composite makeMenuComposite = new Composite(shell, SWT.NONE);
        makeMenuComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        makeMenuComposite.setLayout(new GridLayout(3, false));
        
        menuTable = new Table(makeMenuComposite, SWT.BORDER | SWT.FULL_SELECTION);
        menuTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        menuTable.setHeaderVisible(true);
        menuTable.setLinesVisible(false);
        
        Composite orderControlsComposite = new Composite(makeMenuComposite, SWT.NONE);
        orderControlsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1));
        orderControlsComposite.setLayout(new RowLayout(SWT.VERTICAL));
        
        addMenuBtn = new Button(orderControlsComposite, SWT.CENTER);
        addMenuBtn.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if(courseTable.getSelection().length>0){
                    menuController.addMenuItem(courseTable.getSelection()[0]);
                }
            }
        });
        addMenuBtn.setLayoutData(new RowData(32, 32));
        addMenuBtn.setText("<");
        
        removeMenuBtn = new Button(orderControlsComposite, SWT.CENTER);
        removeMenuBtn.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if(menuTable.getSelection().length>0){
                    menuController.removeMenuItem(menuTable.getSelection()[0]);
                }    
            }
        });
        removeMenuBtn.setLayoutData(new RowData(32, 32));
        removeMenuBtn.setText(">");
        
        courseTable = new Table(makeMenuComposite, SWT.BORDER | SWT.FULL_SELECTION);
        courseTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        courseTable.setHeaderVisible(true);
        courseTable.setLinesVisible(false);
        
        Composite confirmeMenuComposite = new Composite(shell, SWT.NONE);
        confirmeMenuComposite.setLayout(new GridLayout(5, false));
        confirmeMenuComposite.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false, 1, 1));
        
        dateTimeWidget = new DateTime(confirmeMenuComposite, SWT.BORDER | SWT.CALENDAR | SWT.SHORT);
        
        dateTimeWidget.setDate(convertDate(menuController.getExpiryDate())[0], 
                               convertDate(menuController.getExpiryDate())[1], 
                               convertDate(menuController.getExpiryDate())[2]);
        dateTimeWidget.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                int day= dateTimeWidget.getDay();
                int month=dateTimeWidget.getMonth();
                int year= dateTimeWidget.getYear();
                Calendar date = Calendar.getInstance();
                date.set(year, month, day);
                menuController.setExpiryDate(date);
                dateTimeLabel.setText("Меню будет действительно до: "+day+"."+month+"."+year);
                dateTimeLabel.setBounds(dateTimeLabel.getBounds());
            }
        });
        dateTimeWidget.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, false, true, 1, 1));
        
        dateTimeLabel = new Label(confirmeMenuComposite, SWT.NONE);
        dateTimeLabel.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 1, 1));
        dateTimeLabel.setText("Меню будет действительно до: "+dateTimeWidget.getDay()+"."+
                                                              dateTimeWidget.getMonth()+"."+
                                                              dateTimeWidget.getYear());
   
        
        saveMenuBtn = new Button(confirmeMenuComposite, SWT.NONE);
        saveMenuBtn.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                menuController.saveMenuList();
                //TODO messagebox
                shell.dispose();
            }
        });

        saveMenuBtn.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, false, true, 1, 1));
        saveMenuBtn.setToolTipText("");
        saveMenuBtn.setText("Сохранить");
        
        cancelBtn = new Button(confirmeMenuComposite, SWT.NONE);
        cancelBtn.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                shell.dispose();
            }
        });
        cancelBtn.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, false, false, 1, 1));
        cancelBtn.setText("Отмена");

        
        String[] titles = { "Категория","Наименование","Цена"};
        
        for (int i = 0; i < titles.length; i++) {
            TableColumn column = new TableColumn(courseTable, SWT.NONE);
            column.setText(titles[i]);
        }
        
        for (int i = 0; i < titles.length; i++) {
            TableColumn column = new TableColumn(menuTable, SWT.NONE);
            column.setText(titles[i]);
        }
        
        for (int i=0; i<titles.length; i++) {
            menuTable.getColumn(i).pack ();
            courseTable.getColumn(i).pack ();
          }
        
        menuController.loadCourseList();
        menuController.loadMenuList();
        
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
    
    public int[] convertDate(Calendar calendar){
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int[] date = {year,month,day};
        return  date;
    }
}
